package com.cloudservice.ccs.biz.impl;

import com.cloudservice.ccs.biz.IWorkloadStatisticsBiz;
import com.cloudservice.ccs.entity.WorkloadStatistics;
import com.cloudservice.ccs.model.WorkloadStatisticsAggregation;
import com.cloudservice.ccs.model.WorkloadStatisticsWithAverage;
import com.cloudservice.ccs.exception.CustomerServiceException;
import com.cloudservice.ccs.exception.WorkloadStatisticsException;
import com.cloudservice.ccs.mapper.WorkLoadStatisticsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WorkloadStatisticsImpl implements IWorkloadStatisticsBiz {
	@Autowired
	WorkLoadStatisticsMapper workLoadStatisticsMapper;

	@Override
	public void save(WorkloadStatistics workloadStatistics) {
		int result = workLoadStatisticsMapper.save(workloadStatistics);
		if (result == 0) {
			throw new WorkloadStatisticsException("Create CustomerService entity error");
		}
	}

	@Override
	public void update(Integer id, WorkloadStatistics workloadStatistics) {
		int result = workLoadStatisticsMapper.update(id, workloadStatistics);
		if (result == 0) {
			throw new WorkloadStatisticsException("Update CustomerService entity error");
		}
	}

	@Override
	public void delete(Integer id) {
		int result = workLoadStatisticsMapper.delete(id);
		if (result == 0) {
			throw new WorkloadStatisticsException("Delete CustomerService entity error");
		}
	}

	@Override
	public WorkloadStatistics getOne(Integer id) {
		WorkloadStatistics workloadStatistics = workLoadStatisticsMapper.getOne(id);
		if (workloadStatistics == null) {
			throw new WorkloadStatisticsException("Get CustomerService entity error: no such entity");
		}
		return workloadStatistics;
	}

	@Override
	public void partialUpdate(Integer id, Map<String, Object> fields) {

		int result = workLoadStatisticsMapper.partialUpdate(id, fields);
		if (result == 0) {
			throw new CustomerServiceException("访客信息失败");
		}
	}
	@Override
	public List<WorkloadStatistics> getAll() {
		return workLoadStatisticsMapper.getAll();
	}

	@Override
	public List<WorkloadStatisticsWithAverage> getAllWithAverage() {
		List<WorkloadStatistics> workloadStatisticsList = workLoadStatisticsMapper.getAll();
		List<WorkloadStatisticsWithAverage> result = new ArrayList<>();
		for (WorkloadStatistics workloadStatistics : workloadStatisticsList) {
			WorkloadStatisticsWithAverage item = WorkloadStatisticsWithAverage.fromWorkloadStatistics(workloadStatistics);
			item.calculateAverage();
			result.add(item);
		}
		return result;
	}

	@Override
	public WorkloadStatisticsAggregation getAggregation() {
		WorkloadStatisticsAggregation part1 = workLoadStatisticsMapper.getStatisticsPartOfAggregation(),
				part2 = workLoadStatisticsMapper.getSessionPartOfAggregation();
		part1.setMaxSessionTime(part2.getMaxSessionTime());
		part1.setMaxMessageCount(part2.getMaxMessageCount());
		return part1;
	}
}
